package com.pj.wisdomlogistics.ui.view.recyclerview;

import android.content.Context;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by zhang on 2016/9/1.
 */
public class BaseViewHolder extends RecyclerView.ViewHolder {
    private SparseArray<View> views;
    public BaseViewHolder(View itemView) {
        super(itemView);
        views=new SparseArray<>();
    }
    public BaseViewHolder(ViewGroup parent, @LayoutRes int resId) {
        this(LayoutInflater.from(parent.getContext()).inflate(resId,parent,false));
    }

    public <V extends View> V getView(@IdRes int resId){
        View view = views.get(resId);
        if (view == null) {
            view = itemView.findViewById(resId);
            views.put(resId,view);
        }
        return (V) view;
    }

    public <V extends View> V getView(@IdRes int resId,Class<V> viewType){
        View view = views.get(resId);
        if (view == null) {
            view = itemView.findViewById(resId);
            views.put(resId,view);
        }
        return (V) view;
    }

    public TextView getTextView(@IdRes int tvId){
        return getView(tvId);
    }

    public ImageView getImageView(@IdRes int ivId){
        return getView(ivId);
    }

    public Context getContext(){
        return itemView.getContext();
    }
}
